
package editor;
/**
 *  
 * @author Marcin & Bartek
 * 
 * @description Loop for the map editor
 */

import graphic.Camera;
import graphic.Color;
import graphic.Graphic;
import graphic.LoadingFrame;
import graphic.Pointer;
import graphic.Sun;
import graphic.WorldConfig;
import org.lwjgl.opengl.Display;
import input.Input;
import input.InputBox;
import openGL11.Engine;
import org.lwjgl.input.Keyboard;
import objects.Cross;
import objects.Estate;
import objects.Mill;
import objects.Text2dPrinter;
import objects.Triangle;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
/**
 *
 * @author nicra
 */
public class Eloop {
    
    public long fps_t=0;
    public float lfps=0,fps=0;
    
    public Eloop(Engine e){
        boolean lock=false,drawAll=false;
        int editMode=0;
        int editParam=1;
        float ep[]= new float[]{0,0,0,0};
        boolean isMill=false;
        int isEstate=0;
        Cross c = new Cross();
        c.set(0,0,0, 0.5f, 0.15f, new Color(0,0,1,1));
        c.gen();
        e.start3D();
        Text2dPrinter writer = new Text2dPrinter();
        Text2dPrinter arial = new Text2dPrinter();
        Text2dPrinter lvlWr = new Text2dPrinter();
        writer.setFont("GolonkaFA.otf", 30);
        writer.setColor(org.newdawn.slick.Color.yellow);
       
        arial.setFont("HomeRem.ttf", 45);
        arial.setColor(org.newdawn.slick.Color.cyan);
       
        lvlWr.setFont("HomeRem.ttf", 35);
        lvlWr.setColor(org.newdawn.slick.Color.cyan);
        
        LoadingFrame lf = new LoadingFrame();
        lf.updateSplash();
        Sun.set(WorldConfig.DEF_SUN);
        lf.updateSplash();
        WorldQ w= new WorldQ(40f,2f);
        lf.updateSplash();
        Pointer.init();
        lf.updateSplash();

        Input.init();
        lf.updateSplash();
        Camera.addHeight(20);
        lf.updateSplash();
        lf.endSplash();
        InputBox ib = new InputBox(5,200,5,30);
        ib.background=true;
        ib.act=false;
        fps_t=System.nanoTime();
        while((!Display.isCloseRequested())){
        
            Input.refresh();
            
            
            e.clear();
            
            e.start3D();
            
            Camera.refresh();   
            if(Input.ifKeyPressed(Keyboard.KEY_RETURN)){
                if(ib.act){
                    String cmd=ib.txt.split(" ")[0];
                    switch(cmd){
                        case "size":
                            w=new WorldQ(Float.parseFloat(ib.txt.split(" ")[1]),2f);
                            isMill=false;
                            isEstate=0;
                            break;
                        case "tex":
                            editMode=0;
                            editParam=Integer.parseInt(ib.txt.split(" ")[1]);
                            break;
                        case "cp":
                            switch(ib.txt.split(" ")[1]){
                                case "add":
                                    editMode=1;
                                    break;
                                case "del":
                                    editMode=2;
                                    break;
                            }
                            break;
                        case "set":
                            switch(ib.txt.split(" ")[1]){
                                case "mill":
                                    editMode=3;
                                    break;
                                case "estate":
                                    editMode=4;
                                    editParam=Integer.parseInt(ib.txt.split(" ")[2]);
                                    break;
                            }
                            break;
                        case "all":
                            drawAll=!drawAll;
                            break;
                        case "save":
                            w.prepareToSave();
                                w.toFile(ib.txt.split(" ")[1]);
                                /*FileOutputStream fileOut =
                                    new FileOutputStream(ib.txt.split(" ")[1]);
                                ObjectOutputStream out =
                                    new ObjectOutputStream(fileOut);
                                out.writeObject(w);
                                out.close();
                                fileOut.close();*/
                            break;
                        case "load":
                            w.fromFile(ib.txt.split(" ")[1]);
                            isMill=true;
                            isEstate=3;
                            break;
                    }
                }
                ib.act=!ib.act;
            }
            if(!ib.act){
                Camera.getInput();
                Sun.getInput();
            }
            
            if(Input.isKeyDown(Keyboard.KEY_ESCAPE)){
                Display.destroy();
                System.exit(1);
            }
           
            
            
            w.editorDraw(isMill,isEstate,0);
            if(drawAll){
               w.editorDraw(isMill,isEstate,90);
               w.editorDraw(isMill,isEstate,180);
               w.editorDraw(isMill,isEstate,270);
            }
            if(Input.LMBdown){
                Pointer.refresh();
                switch(editMode){
                    case 0:
                        Triangle ed=w.getTriangle(Pointer.x, Pointer.z);
                        if(ed!=null) ed.type=editParam;
                        break;
                    case 1:
                        if(!lock){
                            w.checkPoint.add(new Vector3f(Pointer.x,Pointer.y,Pointer.z));
                            lock=true;
                        }
                        break;
                    case 2:
                        if(!lock){
                            for(int i=0;i<w.checkPoint.size();++i){
                                Vector3f v = w.checkPoint.get(i);
                                if(Math.abs(v.x-Pointer.x)<0.5f && Math.abs(v.z-Pointer.z)<0.5f){
                                    w.checkPoint.remove(i);
                                    break;
                                }
                            }
                            lock=true;
                        }
                        break;
                    case 3:
                        if(!lock){
                            if(!isMill) { 
                                w.mill=new Mill(Pointer.x, Pointer.y, Pointer.z,0.2f);
                                isMill=true;
                            }
                            else w.mill.move(Pointer.x, Pointer.y, Pointer.z);
                            lock=true;
                        }
                        break;
                    case 4:
                        if(!lock){
                            if(editParam==0){
                                ep[0]=Pointer.x;
                                ep[1]=Pointer.z;
                                isEstate|=1;
                         }else{
                                ep[2]=Pointer.x;
                                ep[3]=Pointer.z;
                                isEstate|=2;
                            }
                            if(isEstate==3){
                                w.estate= new Estate(ep[0],ep[1],ep[2],ep[3],3f);
                            }
                            lock=true;
                        }
                        break;
                }
            }else{
                lock=false;
            }
            
            for(int i=0;i<w.checkPoint.size();++i){
                Vector3f v=w.checkPoint.get(i);
                c.move(v.x, v.y, v.z);
                Graphic.e.startDrawT();
                c.draw();
                Graphic.e.endDraw();
                if(i!=0){
                    Vector3f v2=w.checkPoint.get(i-1);
                    GL11.glLineWidth(2f);
                    GL11.glBegin(GL11.GL_LINES);
                    GL11.glVertex3f(v.x, v.y+0.2f, v.z);
                    GL11.glVertex3f(v2.x, v2.y+0.2f, v2.z);
                    GL11.glEnd();
                }
            }
            Pointer.draw();   
            Sun.refresh();
            
            e.start2D();
            ib.refresh();
            switch(editMode){
                case 0:
                    writer.write(250, 10, "tex "+editParam);
                    break;
                case 1:
                    writer.write(250, 10, "cp add "+w.checkPoint.size());
                    break;
                case 2:
                    writer.write(250, 10, "cp del "+w.checkPoint.size());
                    break;
                case 3:
                    writer.write(250, 10, "set mill");
                    break;
                case 4:
                    writer.write(250, 10, "set estate "+editParam);
                    break;
            }
            ++fps;
            if(System.nanoTime()-fps_t>500000000L){
                fps_t=System.nanoTime();
                lfps=fps*2;
                fps=0;
            }
            writer.write(600,10, "FPS: "+lfps);
            Display.update();  
            Display.sync(60);
        }
        Display.destroy();
        System.exit(1);
    }
}
